home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / fl_shortcut.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-11  |  4.0 KB  |  129 lines

  1. //
  2. // "$Id: fl_shortcut.cxx,v 1.4.2.1 1999/05/11 09:39:31 bill Exp $"
  3. //
  4. // Shortcut support routines for the Fast Light Tool Kit (FLTK).
  5. //
  6. // Copyright 1998-1999 by Bill Spitzak and others.
  7. //
  8. // This library is free software; you can redistribute it and/or
  9. // modify it under the terms of the GNU Library General Public
  10. // License as published by the Free Software Foundation; either
  11. // version 2 of the License, or (at your option) any later version.
  12. //
  13. // This library is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Library General Public
  19. // License along with this library; if not, write to the Free Software
  20. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  21. // USA.
  22. //
  23. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  24. //
  25.  
  26. // Code to test and parse fltk shortcut numbers.
  27. //
  28. // A shortcut is a keysym or'd with shift flags.  In the simplest
  29. // sense a shortcut is matched if the shift state is exactly as
  30. // given and the key returning that keysym is pressed.
  31. //
  32. // To make it easier to match some things it is more complex:
  33. //
  34. // Only FL_META, FL_ALT, FL_SHIFT, and FL_CTRL must be "off".  A
  35. // zero in the other shift flags indicates "dont care".
  36. //
  37. // It also checks against the first character of Fl::event_text(),
  38. // and zero for FL_SHIFT means "don't care".
  39. // This allows punctuation shortcuts like "#" to work (rather than
  40. // calling it "shift+3")
  41.  
  42. #include <FL/Fl.H>
  43. #include <FL/Fl_Widget.H>
  44. #include <FL/fl_draw.H>
  45. #include <ctype.h>
  46. #include <string.h>
  47. #ifndef WIN32
  48. #include <X11/Xlib.h>
  49. #endif
  50.  
  51. int Fl::test_shortcut(int shortcut) {
  52.   if (!shortcut) return 0;
  53.  
  54.   int shift = Fl::event_state();
  55.   // see if any required shift flags are off:
  56.   if ((shortcut&shift) != (shortcut&0x7fff0000)) return 0;
  57.   // record shift flags that are wrong:
  58.   int mismatch = (shortcut^shift)&0x7fff0000;
  59.   // these three must always be correct:
  60.   if (mismatch&(FL_META|FL_ALT|FL_CTRL)) return 0;
  61.  
  62.   int key = shortcut & 0xffff;
  63.  
  64.   // if shift is also correct, check for exactly equal keysyms:
  65.   if (!(mismatch&(FL_SHIFT)) && key == Fl::event_key()) return 1;
  66.  
  67.   // try matching ascii, ignore shift:
  68.   if (key == event_text()[0]) return 1;
  69.  
  70.   // kludge so that Ctrl+'_' works (as opposed to Ctrl+'^_'):
  71.   if ((shift&FL_CTRL) && key >= 0x3f && key <= 0x5F
  72.       && event_text()[0]==(key^0x40)) return 1;
  73.   return 0;
  74. }
  75.  
  76. const char * fl_shortcut_label(int shortcut) {
  77.   static char buf[20];
  78.   char *p = buf;
  79.   if (!shortcut) {*p = 0; return buf;}
  80.   if (shortcut & FL_META) {strcpy(p,"Meta+"); p += 5;}
  81.   if (shortcut & FL_ALT) {strcpy(p,"Alt+"); p += 4;}
  82.   if (shortcut & FL_SHIFT) {strcpy(p,"Shift+"); p += 6;}
  83.   if (shortcut & FL_CTRL) {strcpy(p,"Ctrl+"); p += 5;}
  84.   int key = shortcut & 0xFFFF;
  85. #ifdef WIN32
  86.   if (key >= FL_F && key <= FL_F_Last) {
  87.     *p++ = 'F';
  88.     if (key > FL_F+9) *p++ = (key-FL_F)/10+'0';
  89.     *p++ = (key-FL_F)%10 + '0';
  90.   } else {
  91.     if (key == FL_Enter || key == '\r') {strcpy(p,"Enter"); return buf;}
  92.     *p++ = uchar(key);
  93.   }
  94.   *p = 0;
  95.   return buf;
  96. #else
  97.   const char* q;
  98.   if (key == FL_Enter || key == '\r') q="Enter"; // don't use Xlib's "Return"
  99.   else if (key > 32 && key < 0x100) q = 0;
  100.   else q = XKeysymToString(key);
  101.   if (!q) {*p++ = uchar(key); *p = 0; return buf;}
  102.   if (p > buf) {strcpy(p,q); return buf;} else return q;
  103. #endif
  104. }
  105.  
  106. // Tests for &x shortcuts in button labels:
  107.  
  108. int Fl_Widget::test_shortcut(const char *label) {
  109.   char c = Fl::event_text()[0];
  110.   if (!c || !label) return 0;
  111.   for (;;) {
  112.     if (!*label) return 0;
  113.     if (*label++ == '&' && *label) {
  114.       if (*label == '&') label++;
  115.       else if (*label == c) return 1;
  116.       else return 0;
  117.     }
  118.   }
  119. }
  120.  
  121. int Fl_Widget::test_shortcut() {
  122.   if (!(flags()&SHORTCUT_LABEL)) return 0;
  123.   return test_shortcut(label());
  124. }
  125.  
  126. //
  127. // End of "$Id: fl_shortcut.cxx,v 1.4.2.1 1999/05/11 09:39:31 bill Exp $".
  128. //
  129.